//
//  LS_CityTravelViewController.m
//  breadPro
//
//  Created by 刘文超 on 2019/7/4.
//  Copyright © 2019 刘文超. All rights reserved.
//

#import "LS_CityTravelViewController.h"
#import "LS_CityTravelViewModel.h"
#import "LS_Textfield.h"
#import "LS_InfiniteCarouselView.h"
#import "LS_TableView.h"
#import "LS_TableViewBindingHelper.h"
@interface LS_CityTravelViewController ()<UITextFieldDelegate>

/**
 bind ViewModel
 */
@property(nonatomic,strong)LS_CityTravelViewModel *viewModel;

/**
导航左边按钮
 */
@property(nonatomic,strong)UIButton *nav_leftBtn;

/**
 导航右边按钮
 */
@property(nonatomic,strong)UIButton *nav_rightBtn;

/**
 导航中间搜索试图
 */
@property(nonatomic,strong)LS_Textfield *nav_TF;

/**
 是否为搜索
 */
@property(nonatomic,assign)BOOL isSearch;

/**
 轮播图
 */
@property(nonatomic,strong)UIView *headerView;

/**
 banner
 */
@property(nonatomic,strong)LS_InfiniteCarouselView *bannerView;

/**
 tableview
 */
@property(nonatomic,strong)LS_TableView *tableView;
/**
 bind tableview
 */
@property(nonatomic,strong)LS_TableViewBindingHelper *tripBindingHelper;
///**
// banner图数据
// */
//@property(nonatomic,strong)NSMutableArray *bannerData;
@end

@implementation LS_CityTravelViewController
-(UIButton *)nav_leftBtn
{
    return LS_LAZY(_nav_leftBtn, ({
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        [button setImage:LS_Getimage(@"breadTrip_logo") forState:UIControlStateNormal];
        button.frame = CGRectMake(0, 0, 40, 40);
        button.adjustsImageWhenHighlighted = NO;
        button;
        
    }));
}
-(UIButton *)nav_rightBtn
{
    return LS_LAZY(_nav_rightBtn, ({
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        button.frame = CGRectMake(0, 0, 30, 20);
        [button setImage:LS_Getimage(@"tabbar_user_button_image_22x22_") forState:UIControlStateNormal];
        button.titleLabel.font = LS_FontWithname(@"DamascusLight", 14.0);
        button;
    }));
}
-(LS_Textfield *)nav_TF
{
    return LS_LAZY(_nav_TF, ({
        LS_Textfield *textfield = [[LS_Textfield alloc]init];
        textfield.frame = CGRectMake(15, 8, ScreenWidth-140, 30);
        textfield.delegate =self;
        textfield;
    }));
}
-(UIView *)headerView
{
    return LS_LAZY(_headerView, ({
        UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, 180)];
        [view addSubview:self.bannerView];
        view;
    }));
}
-(LS_InfiniteCarouselView *)bannerView
{
    return LS_LAZY(_bannerView, ({
        LS_InfiniteCarouselView *infiniteView = [[LS_InfiniteCarouselView alloc]initWithFrame:CGRectMake(0, 10, ScreenWidth, 170)];
        infiniteView.autoScrollTimeInterval = 0.1;
        infiniteView.cornerRadius =5;
        infiniteView;
    }));
}
//-(NSMutableArray *)bannerData
//{
//    return LS_LAZY(_bannerData,@[].mutableCopy);
//}
-(LS_TableView *)tableView
{
    return LS_LAZY(_tableView, ({
        LS_TableView *tableview = [[LS_TableView alloc]initWithFrame:self.view.bounds];
        tableview.tableHeaderView = self.headerView;
        tableview.rowHeight = 180;
        [self.view addSubview:tableview];
        tableview;
    }));
}
-(instancetype)initWithViewModel:(LS_CityTravelViewModel *)viewModel
{
    if (self = [super init]) {
        self.viewModel = viewModel;
    }return self;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    [self setNav];
    
    [self bindView];
 
}
-(void)bindView
{
    //搜索框
    self.isSearch = NO;
    @weakify(self);
    [[self rac_signalForSelector:@selector(textFieldDidBeginEditing:) fromProtocol:@protocol(UITextFieldDelegate)] subscribeNext:^(RACTuple *tuple) {
        @strongify(self)
        if (tuple.first == self.nav_TF) {
            [self.nav_rightBtn setImage:LS_Getimage(@"") forState:UIControlStateNormal];
            [self.nav_rightBtn setTitle:@"取消" forState:UIControlStateNormal];
            self.isSearch = YES;
        }
    }];
    [[self rac_signalForSelector:@selector(textFieldDidEndEditing:) fromProtocol:@protocol(UITextFieldDelegate)] subscribeNext:^(RACTuple *tuple) {
        @strongify(self)
        if (tuple.first == self.nav_TF) {
            [self.nav_rightBtn setImage:LS_Getimage(@"tabbar_user_button_image_22x22_") forState:UIControlStateNormal];
            [self.nav_rightBtn setTitle:@"" forState:UIControlStateNormal];
            self.isSearch = NO;
        }
    }];
    
    //RAC(self.viewModel , isSearch) 用于给某个对象的某个属性绑定
    //RACObserve(self, isSearch) 监听某个对象的属性 返回的是信号
    RAC(self.viewModel , isSearch) = RACObserve(self, isSearch);
    
    //导航栏navigationItem
    self.nav_rightBtn.rac_command =  self.viewModel.rightBarButtonItemCommand;
    [self.viewModel.rightBarButtonItemCommand.executionSignals.switchToLatest subscribeNext:^(NSNumber  *isSearch) {
        @strongify(self)
        if ([isSearch boolValue]) {
            [self.nav_TF resignFirstResponder];
        }else{
            NSLog(@"进入个人中心");
        }
   }];
    
    //数据请求
    [self.viewModel.travelCommand execute:@(1)];
    self.bannerView.imageUrlSignal = RACObserve(self.viewModel, bannerData);
    
    self.tripBindingHelper = [LS_TableViewBindingHelper bindingHelperForTableView:self.tableView sourceSignal:RACObserve(self.viewModel, travelData) selectCommand:nil templateCell:@"LS_CitytravelTableViewCell" withViewModel:self.viewModel];
 
}
-(void)setNav
{
    UIBarButtonItem *leftItem = [[UIBarButtonItem alloc]initWithCustomView:self.nav_leftBtn];
    self.navigationItem.leftBarButtonItem = leftItem;
    UIBarButtonItem *rightItem = [[UIBarButtonItem alloc]initWithCustomView:self.nav_rightBtn];
     self.navigationItem.rightBarButtonItem = rightItem;
    
    UIView *navView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, 44)];
    [navView addSubview:self.nav_TF];
    self.navigationItem.titleView =navView;
}


@end
